//doorlever.txt - A controller for a nearby door.
//Cell 0 - The difficulty. If 0, none. Otherwise, the level which must be overcome
//to open it. If tool use skill is at least difficulty, opens. Otherwise, 
//requires use of (Difficulty / Tool Use + 1) keys.
// If this is 99, player has to have key.
//Cell 1 - The required key. If 0, none. Otherwise, the number of the special item required to open the door.
//Cell 2,3 - The sdf which is set to 1 when lever is unlocked.
//Cell 4 - Range of unlock message. If left at 0, uses range of 3.

beginobjectscript; // door lever

variables;
	short i_opened_door = 0;
	short i_am_unlocked = 0;
	short range = 0;
body;

beginstate INIT_STATE;
	set_obj_tool_difficulty(ME,get_memory_cell(0));
	if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0)) {
		if (get_sdf(get_memory_cell(2),get_memory_cell(3)) > 0) {
			i_am_unlocked = 1;
			set_obj_tool_difficulty(ME,0);
			}
		}
	break;

beginstate DEAD_STATE;

break;

beginstate START_STATE; // door closed, waiting
break;

beginstate USE_STATE;
	range = get_memory_cell(4);
	if (range == 0)
		range = 3;
		
	if (i_opened_door > 0) {
		print_str("Pull Lever: The lever is stuck in this position.");
		end();
		}
	
	if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0)) {
		if (get_sdf(get_memory_cell(2),get_memory_cell(3)) > 0)
			i_am_unlocked = 1;
		}
	if ((get_memory_cell(1) > 0) && (get_obj_tool_difficulty(ME) > 0) && (i_am_unlocked == 0)) {
		if (has_spec_item(get_memory_cell(1))) {
			print_str_color("Pull Lever: One of your keys unlocks the chains. You pull the lever.",2);
			play_sound(171);
			i_opened_door = 1;
			broadcast_object_message(range,100);
			set_object_icon(ME,11);
			if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0))
				set_flag(get_memory_cell(2),get_memory_cell(3),1);
			end();
			}
		}

	if ((get_obj_tool_difficulty(ME) > 0) && (i_am_unlocked == 0) && (get_memory_cell(0) >= 90)) {
		print_str_color("Pull Lever: The chains around the lever are locked. You don't have the key, and",2);
		print_str_color("  the lock is too complicated to be picked.",2);
		end();
		}
	

	if ((get_obj_tool_difficulty(ME) > 0) && (i_am_unlocked == 0)) {
		if (get_obj_num_tools_needed(ME) <= 0) {
			print_str_color("Pull Lever: The lever is held in position by locked chains, but you",2);
			print_str_color("  manage to pick the lock.",2);
			play_sound(171);
			i_opened_door = 1;
			broadcast_object_message(range,100);
			set_object_icon(ME,11);
			
			if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0)) {
				if (get_flag(get_memory_cell(2),get_memory_cell(3)) == 0)
					award_party_xp(BASE_TRAP_XP,trap_lev_to_xp_lev(get_memory_cell(0)));
				set_flag(get_memory_cell(2),get_memory_cell(3),1);
				}
			end();			
			}
			else {
				if (get_memory_cell(0) >= 99) {
					print_str_color("Pull Lever: You can't pull the lever. You don't have the key, and",2);
					print_str_color("  the lock is too complicated to be picked.",2);
					end();
					}	
				if (num_of_item(255) < get_obj_num_tools_needed(ME)) {
					if (get_memory_cell(1) > 0) {
						print_str_color("Pull Lever: The lever is locked in this position. You don't have the key, and",2);
						print_big_str_color("  you don't have enough Living Tools to move it. (You need ",get_obj_num_tools_needed(ME),".)",2);
						}
						else {
							print_str_color("Pull Lever: The lever is locked in this position, and you don't have enough",2);
							print_big_str_color("   Living Tools to move it. (You need ",get_obj_num_tools_needed(ME),".)",2);
							}
					end();
					}
				if (get_memory_cell(1) > 0) 
					show_object_disarm_window(ME,"The lever is held by locked chains, and you don't have the key. However, you could use Living Tools to attempt to open it.");
					else show_object_disarm_window(ME,"The lever is held by locked chains. You could use Living Tools to attempt to open it.");	
				}
		end();
		}

	if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0))
		set_flag(get_memory_cell(2),get_memory_cell(3),1);
	print_str_color("Pull Lever: You pull the lever.",2);
	i_opened_door = 1;
	broadcast_object_message(range,100);
	play_sound(246);
	set_object_icon(ME,11);
break;

beginstate UNLOCK_STATE;
	if (i_opened_door > 0) {
		print_str_color("Unlock Spell: The lever has already been pulled.",2);
		end();
		}
	if (current_unlock_strength() >= get_memory_cell(0)) {
		print_str_color("Unlock Spell: The spell loosens the lock. You can pull the lever now.",2);
		set_memory_cell(0,0);
		set_obj_tool_difficulty(ME,0);
		play_sound(171);
		end();
		}
	print_str_color("Unlock Spell: The spell is too weak to affect the lock.",2);	
break;

beginstate TOOLS_USED_STATE;
	play_sound(171);
	i_opened_door = 1;
	broadcast_object_message(range,100);
	set_object_icon(ME,11);
	
	if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0)) {
		if (get_flag(get_memory_cell(2),get_memory_cell(3)) == 0)
			award_party_xp(BASE_TRAP_XP,trap_lev_to_xp_lev(get_memory_cell(0)));
		set_flag(get_memory_cell(2),get_memory_cell(3),1);
		}
break;